//
//  SchoolViewController.m
//  BOSS-SOOC
//
//  Created by apple on 16/8/12.
//  Copyright © 2016年 Gcx. All rights reserved.
//

#import "SchoolViewController.h"
#import "SchoolViewModel.h"
#import "SchoolTableView.h"
#import "BannerModel.h"
#import "UIButton+comment.h"
#import "ClassViewController.h"
#import "SearchViewController.h"

@interface SchoolViewController ()<SchoolTableViewDelegate>

@property (nonatomic,strong) NSArray *modelArray;

/**
 *  页数
 */
@property(nonatomic, assign)long pageNumber;

@end

@implementation SchoolViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self setNavBarTitle:@"主页"];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self creatTableViewAndGetSourceData];
    
    /**
     *  导航栏的左右按钮
     */
    [self addBtnItemLeftAndRight];
}

- (void)addBtnItemLeftAndRight{
    
    UIButton *leftBtn = [UIButton initWithFrame:CGRectMake(0, 0, ScaleSize(66), ScaleSize(66)) type:UIButtonTypeCustom title:nil image:@"课程分类icon.png" selectImage:@"课程分类icon.png"];
    
    [self.navbar setLeftBtn:leftBtn];
    
    UIButton *rightBtn = [UIButton initWithFrame:CGRectMake(0, 0, ScaleSize(66), ScaleSize(66)) type:UIButtonTypeCustom title:nil image:@"搜索icon.png" selectImage:@"搜索icon.png"];
    
    [self.navbar setRightBtn:rightBtn];
    
    /**
     *  添加按钮的点击事件
     */
    
    [leftBtn addTarget:self action:@selector(leftBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn addTarget:self action:@selector(rightBtnClicked:) forControlEvents:UIControlEventTouchUpInside];

}

- (void)leftBtnClicked:(UIButton *)btn{

    ClassViewController * class = [[ClassViewController alloc]init];
    [self navigationCanDragBack:YES];
    class.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:class animated:YES];
}

- (void)rightBtnClicked:(UIButton *)btn{
    
    SearchViewController * search = [[SearchViewController alloc]init];
    [self navigationCanDragBack:YES];
    search.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:search animated:YES];
}

- (void)creatTableViewAndGetSourceData{

    SchoolTableView *tableView = [[SchoolTableView alloc]initWithFrame:CGRectMake(0, 64, SIZEWIDTH, SIZEHEIGHT-108)];
    
    tableView.myDelegate = self;
    
    [self.view addSubview:tableView];
    
    _pageNumber = 1;
    
    SchoolViewModel *schoolModel = [[SchoolViewModel alloc]init];
    
    /**
     *  shcoolList 数据
     */
    [schoolModel getSchoolDatePage:_pageNumber];
    
    tableView.myBlock = ^(BOOL isRefresh){
        
        if (isRefresh == YES) {
            
            _pageNumber = 1;
            
        } else {
            
            _pageNumber++;
        }
        
        [schoolModel getSchoolDatePage:_pageNumber];
        
    };
    
    
    /**
     *  banner 数据
     */
    [schoolModel getBannerDate];
    
    /**
     *  banner的回调
     */
    schoolModel.succeedBannerBlock = ^(id succeedBanner){
        
        tableView.bannerArr = succeedBanner;
        
    };
    
    schoolModel.FailBannerBlock = ^(id failBanner){
        
        NSLog(@"fail == %@", failBanner);
        
    };
    
    /**
     *  校内列表的回调
     */
    schoolModel.succeedBlock = ^(id succeedValue){
        
        _modelArray = succeedValue;
        
        tableView.sourceArr = succeedValue;
    };
    
    schoolModel.failBlock = ^(id failValue){
        
        NSLog(@"error == %@", failValue);
    };

}

/**
 *  cell的点击事件
 */
- (void)schoolTableViewSelected:(SchoolTableView *)schoolTableView resultString:(NSString *)resultString Row:(int)row{

    SchoolViewModel *shoolViewModel = [[SchoolViewModel alloc]init];
    [self navigationCanDragBack:YES];
    [shoolViewModel schoolDetailWithSchoolModel:_modelArray[row] WithViewController:self];
    
}

- (void)bannerSelected:(BannerModel *)bannerModel{

    NSLog(@"bannerModel == %@", bannerModel.imageurl);
}

- (NSArray *)modelArray{

    if (_modelArray) {
        
        _modelArray = [NSArray array];
    }
    
    return _modelArray;
}


@end
